#include "QQRTCAudioEngineByAgora.h"

QQRTCAudioEngineByAgora::QQRTCAudioEngineByAgora(QObject* parent)
	: QQRTCAudioEngine(parent)
{
	mRtcEngine = createAgoraRtcEngine();
}

QQRTCAudioEngineByAgora::~QQRTCAudioEngineByAgora()
{
	if (mInitialize) {
		mRtcEngine->leaveChannel();
		mRtcEngine->release();
	}
	if (mRtcEngine) {
		delete mRtcEngine;
	}
}

void QQRTCAudioEngineByAgora::startRealCommunicateAudio(const QVariantMap& info)
{
	agora::rtc::RtcEngineContext context;
	context.appId = APP_ID;

	if (mRtcEngine->initialize(context)) {
		mInitialize = true;
	}
	else {
		mInitialize = false;
	}

	agora::rtc::ChannelMediaOptions option;
	option.channelProfile = agora::CHANNEL_PROFILE_COMMUNICATION;
	option.clientRoleType = agora::rtc::CLIENT_ROLE_BROADCASTER;
	option.publishMicrophoneTrack = true;
	option.autoSubscribeAudio = false;

	mRtcEngine->joinChannelWithUserAccount(
		info["token"].toString().toUtf8(),
		info["channel"].toString().toStdString().c_str(),
		info["account"].toString().toStdString().c_str(),
		option);
}

void QQRTCAudioEngineByAgora::stopRealCommunicateAudio(const QVariantMap& info)
{
	Q_UNUSED(info);

	if (mInitialize) {
		mRtcEngine->leaveChannel();
		mRtcEngine->release();
		mInitialize = false;
	}
}